﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sahit.iSoilHotel.Application.MainBoundedContext.Tavern.Interfaces;
using Sahit.iSoilHotel.Domain.MainBoundedContext.Tavern.Aggregates.AuthenticationAgg;
using Sahit.iSoilHotel.Application.Seedwork;
using Sahit.iSoilHotel.Application.MainBoundedContext.DTO.AuthenticationAgg;
using Sahit.iSoilHotel.Domain.Seedwork;

namespace Sahit.iSoilHotel.Application.MainBoundedContext.Tavern.Services
{
    public class PermissionService : IPermissionService
    {
        #region Members

        private readonly IRepository<Permission> permissionRepository;

        #endregion

        public PermissionService(
            IRepository<Permission> permissionRepository)
        {
            this.permissionRepository = permissionRepository;
        }

        #region Constructor

        #endregion

        #region IPermissionService.Methods

      
        public void DeletePermission(PermissionDTO permission)
        {
            Permission p = this.permissionRepository.Get(permission.ID);
            this.permissionRepository.Remove(p);
            this.permissionRepository.UnitOfWork.Commit();
        }

        public PermissionDTO GetPermissionById(Guid permissionId)
        {
             var permission = this.permissionRepository.Get(permissionId);
             return permission.ProjectedAs<PermissionDTO>();
        }

        public PermissionDTO GetPermissionBySystemName(string systemName)
        {
            var permission = this.permissionRepository.GetFiltered(p => p.SystemName.Equals(systemName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault<Permission>();
            return permission.ProjectedAs<PermissionDTO>();
        }

        public IList<PermissionDTO> GetAllPermissions()
        {
            return this.permissionRepository.GetAll().ProjectedAsCollection < PermissionDTO>();
        }

        public void InsertPermission(PermissionDTO permission)
        {
            Permission p = new Permission()
            {
                Name = permission.Name,
                SystemName = permission.SystemName,
                Category = permission.Category
            };
            p.GenerateNewIdentity();
            this.permissionRepository.Add(p);
            this.permissionRepository.UnitOfWork.Commit();
        }

        public void UpdatePermission(PermissionDTO permission)
        {
            Permission persisted = this.permissionRepository.Get(permission.ID);
            Permission current = new Permission()
            {
                Name = permission.Name,
                SystemName = permission.SystemName,
                Category = permission.Category
            };
            foreach (AccountRoleDTO role in permission.AccountRoles)
                current.AddNewAccountRole(role.ID, role.Name);
            current.ChangeCurrentIdentity(permission.ID);
            this.permissionRepository.Merge(persisted, current);
        }

        public bool Authorize(PermissionDTO permission)
        {
            return true;
        }

        public bool Authorize(PermissionDTO permission, AccountDTO account)
        {
            throw new NotImplementedException();
        }

        public bool Authorize(string permissionRecordSystemName)
        {
            throw new NotImplementedException();
        }

        public bool Authorize(string permissionRecordSystemName, AccountDTO account)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
